package cn.itcast.bos.web.action.base;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;

@Namespace("/")
@ParentPackage("struts-default")
@Scope("prototype")
@Controller
public class StandardAction extends ActionSupport implements ModelDriven<Standard> {
	private static final long serialVersionUID = 1L;
	private Standard standard = new Standard();
	@Autowired
	private StandardService standardService;

	@Override
	public Standard getModel() {
		return standard;
	}

	// 添加
	@Action(value = "standard_add", results = {
			@Result(name = "success", location = "./pages/base/standard.html", type = "redirect") })
	public String add() {
		standardService.add(standard);
		return SUCCESS;
	}

	// 分页查找
	private Integer page;// 接受页面发送过来的页码
	private Integer rows;// 接受页面发过来的每页显示的记录数

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	@Action(value = "standard_pageQuery")
	public void pageQuery() throws IOException {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Standard> pageData = standardService.findPageData(pageable);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", pageData.getTotalElements()); // 总记录数
		map.put("rows", pageData.getContent()); // 当前页的总记录
		String jsonString = JSONArray.toJSONString(map);
		ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(jsonString);
	}

	// 查找所有
	@Action(value = "standard_findAll")
	public void findAll() throws IOException {
		List<Standard> list = standardService.findAll();
		String jsonString = JSONArray.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect);
		ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(jsonString);
	}

	// 删除
	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Action(value = "standard_del", results = {
			@Result(name = "success", location = "./pages/base/standard.html", type = "redirect") })
	public String del() {
		String[] idsArr = ids.split("-");
		standardService.del(idsArr);
		return SUCCESS;
	}
}
